﻿Public Class frmMain
    Dim ldsMainData As dsMain

    Private Sub frmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        LaodFrom()
    End Sub

    Sub LaodFrom()
        Try
            Dim oData As New clsData
            ldsMainData = oData.LoadData
            
            LoadTreeView()
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Sub LoadTreeView()
        Try
            tvSystems.Nodes.Clear()
            Dim dRows() As dsMain.SystemsRow = ldsMainData.Systems.Select("Level =1")

            For Each ldr As dsMain.SystemsRow In dRows
                Dim tvNode As TreeNode = tvSystems.Nodes.Add(ldr.SystemID.ToString, ldr.Name)
                tvNode.Tag = ldr.SystemID.ToString
                Dim drDays() As dsMain.SystemsRow = ldsMainData.Systems.Select("ParentID = " & ldr.SystemID & _
                                                                               " AND  Level = 2")
                For Each ldrDays As dsMain.SystemsRow In drDays
                    Dim dayNode As TreeNode = tvNode.Nodes.Add(ldrDays.SystemID.ToString, ldrDays.Name)
                    dayNode.Tag = ldrDays.SystemID.ToString
                Next
            Next
        Catch ex As Exception
            Throw ex
        End Try

    End Sub

    Private Sub AddNewDayToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

        If tvSystems.SelectedNode.Level = 0 Then
            Dim dr As dsMain.SystemsRow = ldsMainData.Systems.NewRow
            dr.Name = FormatDateTime(DateTime.Today, DateFormat.ShortDate)
            dr.ParentID = tvSystems.SelectedNode.Tag
            dr.Level = 2
            ldsMainData.Systems.Rows.Add(dr)
            Dim tvNode As TreeNode = tvSystems.SelectedNode.Nodes.Add(dr.SystemID.ToString, dr.Name)
            tvNode.Tag = dr.SystemID.ToString
            tvSystems.SelectedNode.Expand()
            tvNode.BeginEdit()
        Else
            MessageBox.Show("Select System to add new day to", "Select", MessageBoxButtons.OK)
        End If

    End Sub

    Private Sub SaveToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripButton.Click
        Dim oData As New clsData
        For Each sysNode As TreeNode In tvSystems.Nodes
            Dim dRows() As dsMain.SystemsRow = ldsMainData.Systems.Select("SystemID =" & sysNode.Tag)
            dRows(0).Name = sysNode.Text
            For Each DayNode As TreeNode In sysNode.Nodes
                Dim dayRows() As dsMain.SystemsRow = ldsMainData.Systems.Select("SystemID =" & DayNode.Tag)
                dayRows(0).Name = DayNode.Text
            Next
        Next
        oData.SaveData(ldsMainData)
    End Sub

    Private Sub tvSystems_AfterLabelEdit(ByVal sender As System.Object, ByVal e As System.Windows.Forms.NodeLabelEditEventArgs) Handles tvSystems.AfterLabelEdit

        If IsDate(e.Label) And (e.Node.Level = 1) Then
            Debug.Print(e.Label & " " & e.Node.Tag)
            Dim dayRows() As dsMain.SystemsRow = ldsMainData.Systems.Select("SystemID =" & e.Node.Tag)
            dayRows(0).Name = e.Label
        Else
            If e.Node.Level = 1 Then
                e.CancelEdit = True
                MessageBox.Show("Must Be date", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        End If
        
    End Sub

    Private Sub tvSystems_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles tvSystems.AfterSelect
        If tvSystems.SelectedNode.Level = 0 Then
            'DmnCalender21.DrawBackGround()
            'DmnCalender1.SystemID = tvSystems.SelectedNode.Tag
            'DmnCalender1.dvEvents = ldsMainData.System_Events.DefaultView
            'DmnCalender1.dvDayProperties = ldsMainData.TimeSlotProperties.DefaultView
            'DmnCalender1.Show()
        End If
    End Sub

    Private Sub DmnCalender1_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs)

    End Sub

    Private Sub DmnCalender1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)

    End Sub

    Private Sub DmnCalender1_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs)

    End Sub


    Private Sub DmnCalender1_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub DmnCalender1_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub DmnCalender1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)

    End Sub

    Private Sub DmnCalender1_Move(ByVal sender As Object, ByVal e As System.EventArgs)

    End Sub

 
End Class
